Ext.define('Intranet.VeCongTy.view.AddTab', {
    extend: 'Ext.form.Panel',
    alias: 'widget.VeCongTy_AddTab',
    bodyPadding: 5,
    width: 300,
    height: 100,
    // The form will submit an AJAX request to this URL when submitted
    //url: 'save-form.php',

    // Fields will be arranged vertically, stretched to full width
    layout: 'anchor',
    defaults: {
        anchor: '100%'
    },
    // The fields
    defaultType: 'textfield',
    items: [{
            fieldLabel: 'Tab name',
            name: 'name',
            allowBlank: false

        }, {
            fieldLabel: 'Machine name',
            name: 'key_name',
            allowBlank: false

        }],
    buttons: [{
            text: 'Clear',
            handler: function() {
                this.up('form').getForm().reset();
            }
        }, {
            text: 'OK',
            formBind: true, //only enabled once the form is valid
            disabled: true,
            handler: function() {
                var form = this.up('form').getForm();

                if (form.isValid()) {

                    var panel = Ext.getCmp('VeCongTy_AdminView_TabPanel');

                    var name = form.findField("name").getValue();
                    var key_name = form.findField("key_name").getValue();

                    Ext.Ajax.request({
                        url: '/src/public/ve-cong-ty/tab/addtab',
                        params: {
                            key_name: key_name,
                            name: name
                        },
                        success: function(response, opts) {
                            var obj = Ext.decode(response.responseText);

                            if (obj.success === 'true')
                            {
                                var newtab = Ext.create('Ext.panel.Panel', {
                                    title: name,
                                    id: 'VeCongTy_' + key_name,
                                    tabConfig: {
                                        tooltip: name
                                    },
                                    layout: 'fit',
                                    items: [{
                                            xtype: 'htmleditor',
                                            enableColors: false,
                                            enableAlignments: false,
                                            flex: 1
                                        }],
                                    bbar: ['->', {
                                            xtype: 'button',
                                            text: 'Save',
                                            margin: '0 40 0 0',
                                            handler: function(btn) {
                                                var parent_panel = this.up('panel');
                                                var key_name = parent_panel.getId().replace('VeCongTy_', '');

                                                Ext.Ajax.request({
                                                    url: '/src/public/ve-cong-ty/tab/savevalue',
                                                    params: {
                                                        key_name: key_name,
                                                        value: parent_panel.items.getAt(0).getValue()
                                                    },
                                                    success: function(response, opts) {
                                                        var obj = Ext.decode(response.responseText);

                                                        if (obj.success === 'true')
                                                        {
                                                            Ext.Msg.alert('Save', 'Save successful!');
                                                        }
                                                    },
                                                    failure: function(response, opts) {
                                                        Ext.Msg.alert('Error:', response.status);
                                                    }

                                                });
                                            }
                                        }]


                                });

                                panel.add(newtab);

                                panel.setActiveTab(panel.items.length - 1);

                                var window = Ext.getCmp('VeCongTy_AdminView_AddTabWindow');
                                window.destroy();
                            }
                            else
                            {
                                Ext.Msg.alert('Error:', obj.msg);
                            }

                        },
                        failure: function(response, opts) {
                            console.log('Add tab failed: ' + response.status);
                        }
                    });

                }
            }
        }]

});